{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "c700b01d-b02c-4b41-9d6b-e36e3e2b821f",
   "metadata": {},
   "source": [
    "为了实现在多个平台发布同样的文章，需要有提效的工具，为此初步使用下面的脚本来实现。\n",
    "\n",
    "- 获取目标内容，更多是 OpenAtom 现在的渠道上的文章。比如公众号上、官网等。\n",
    "- 发布到其他的渠道，比如探索、社区等。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9601fce7-af88-4075-839a-151f14956ceb",
   "metadata": {},
   "source": [
    "# 1、获取目标内容\n",
    "目前主要是公众号"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "292964ed-8915-4436-b09d-5f89abf3bd75",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[OK] Reading target URLs from target_urls.txt...\n",
      "[OK] Starting to fetch content for URL 1: https://mp.weixin.qq.com/s/8FEY_nrotujrsIXYV4Ojxg\n",
      "[OK] Fetching content from: https://r.jina.ai/https://mp.weixin.qq.com/s/8FEY_nrotujrsIXYV4Ojxg\n",
      "[OK] Content fetched successfully.\n",
      "[OK] Starting to write content to output/20240704/1.md...\n",
      "[OK] Content saved to output/20240704/1.md\n",
      "[OK] Starting to write content to output/20240704/1.txt...\n",
      "[OK] Content saved to output/20240704/1.txt\n",
      "[OK] Finished processing URL 1. Time taken: 133.94 seconds.\n",
      "[OK] All Done.\n"
     ]
    }
   ],
   "source": [
    "import requests\n",
    "import markdown\n",
    "import time\n",
    "import os\n",
    "from datetime import datetime\n",
    "\n",
    "def get_content(url):\n",
    "    try:\n",
    "        # 拼接基础 URL 和目标 URL\n",
    "        full_url = f\"https://r.jina.ai/{url}\"\n",
    "        print(f\"[OK] Fetching content from: {full_url}\")\n",
    "        # 发起 GET 请求\n",
    "        response = requests.get(full_url)\n",
    "        # 检查请求是否成功\n",
    "        if response.status_code == 200:\n",
    "            print(\"[OK] Content fetched successfully.\")\n",
    "            return response.text\n",
    "        else:\n",
    "            raise Exception(f\"Failed to retrieve content. Status code: {response.status_code}\")\n",
    "    except Exception as e:\n",
    "        print(f\"[Failed] An error occurred: {e}\")\n",
    "        return None\n",
    "\n",
    "def save_to_file(directory, filename, content):\n",
    "    try:\n",
    "        # 拼接文件路径\n",
    "        filepath = os.path.join(directory, filename)\n",
    "        \n",
    "        print(f\"[OK] Starting to write content to {filepath}...\")\n",
    "        with open(filepath, 'w', encoding='utf-8') as f:\n",
    "            f.write(content)\n",
    "        print(f\"[OK] Content saved to {filepath}\")\n",
    "    except Exception as e:\n",
    "        print(f\"[Failed] Failed to save content to {filepath}. Error: {e}\")\n",
    "\n",
    "def main():\n",
    "    # 获取当前日期\n",
    "    current_date = datetime.now().strftime('%Y%m%d')\n",
    "\n",
    "    # 创建当日日期文件夹\n",
    "    output_dir = os.path.join('output', current_date)\n",
    "    if not os.path.exists(output_dir):\n",
    "        os.makedirs(output_dir)\n",
    "\n",
    "    # 读取目标 URL 文件\n",
    "    print(\"[OK] Reading target URLs from target_urls.txt...\")\n",
    "    with open('target_urls.txt', 'r', encoding='utf-8') as f:\n",
    "        target_urls = f.readlines()\n",
    "\n",
    "    for idx, target_url in enumerate(target_urls, start=1):\n",
    "        target_url = target_url.strip()\n",
    "        if not target_url:\n",
    "            continue\n",
    "\n",
    "        print(f\"[OK] Starting to fetch content for URL {idx}: {target_url}\")\n",
    "        start_time = time.time()\n",
    "\n",
    "        # 获取内容\n",
    "        content = get_content(target_url)\n",
    "\n",
    "        if content:\n",
    "            # 生成文件名\n",
    "            base_filename = f\"{idx}\"\n",
    "            md_filename = f\"{base_filename}.md\"\n",
    "            txt_filename = f\"{base_filename}.txt\"\n",
    "\n",
    "            # 保存结果到 Markdown 文件\n",
    "            save_to_file(output_dir, md_filename, content)\n",
    "\n",
    "            # 将 Markdown 转换为 HTML\n",
    "            html_content = markdown.markdown(content)\n",
    "\n",
    "            # 保存结果到 txt 文件\n",
    "            save_to_file(output_dir, txt_filename, html_content)\n",
    "\n",
    "        end_time = time.time()\n",
    "        print(f\"[OK] Finished processing URL {idx}. Time taken: {end_time - start_time:.2f} seconds.\")\n",
    "\n",
    "    print(\"[OK] All Done.\")\n",
    "\n",
    "if __name__ == \"__main__\":\n",
    "    main()\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0b552786-1c8e-4754-9c73-a2948bee5d51",
   "metadata": {},
   "source": [
    "# 2、文章发布到运营专区"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "bda60914-cfe6-4316-bec1-63043853c1b5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[OK] Headers loaded successfully from header.json.\n",
      "[OK] Reading content from output/20240704/1.txt...\n",
      "[OK] Content read successfully from output/20240704/1.txt.\n",
      "[OK] Replacing image URLs...\n",
      "[OK] Uploading image: https://mmbiz.qpic.cn/mmbiz_png/ibVdQeHFSRattLamJYoM9kDh2Jvv4YZMajR4WvtnlGeG3FnwGvtSOPza9rsppmdzNoGpSeJHOWISqxGicyjq4lQQ/640.png\n",
      "[OK] Uploading image url: http://api.pop.atomgit.com/admin/oss/getOssPathByUrl?url=https://mmbiz.qpic.cn/mmbiz_png/ibVdQeHFSRattLamJYoM9kDh2Jvv4YZMajR4WvtnlGeG3FnwGvtSOPza9rsppmdzNoGpSeJHOWISqxGicyjq4lQQ/640.png\n",
      "[OK] Image uploaded successfully: https://file.pop.atomgit.com/imgs/openatom/2024/07/04/1f9fe4b50da5464287c4c63f7262280d.png\n",
      "[OK] Uploading image: https://mmbiz.qpic.cn/mmbiz_png/ibVdQeHFSRattLamJYoM9kDh2Jvv4YZMaia3XpmkXGWupfyqjFNw5ibJiaibibbhZIoQiacBuuWUdFFGEUMwRt9Bxf1JQ/640.png\n",
      "[OK] Uploading image url: http://api.pop.atomgit.com/admin/oss/getOssPathByUrl?url=https://mmbiz.qpic.cn/mmbiz_png/ibVdQeHFSRattLamJYoM9kDh2Jvv4YZMaia3XpmkXGWupfyqjFNw5ibJiaibibbhZIoQiacBuuWUdFFGEUMwRt9Bxf1JQ/640.png\n",
      "[OK] Image uploaded successfully: https://file.pop.atomgit.com/imgs/openatom/2024/07/04/cdc5aa6452f84729b2e176f7094fd8c2.png\n",
      "[OK] Uploading image: https://mmbiz.qpic.cn/mmbiz_png/ibVdQeHFSRattLamJYoM9kDh2Jvv4YZMatRFfH3C2fVOoNUneBbjDOib6c2pZ654IRFvGen8of1SlJh3DB3td26w/640.png\n",
      "[OK] Uploading image url: http://api.pop.atomgit.com/admin/oss/getOssPathByUrl?url=https://mmbiz.qpic.cn/mmbiz_png/ibVdQeHFSRattLamJYoM9kDh2Jvv4YZMatRFfH3C2fVOoNUneBbjDOib6c2pZ654IRFvGen8of1SlJh3DB3td26w/640.png\n",
      "[OK] Image uploaded successfully: https://file.pop.atomgit.com/imgs/openatom/2024/07/04/eab5890c564645e196332376550e4d45.png\n",
      "[OK] Uploading image: https://mmbiz.qpic.cn/mmbiz_png/ibVdQeHFSRattLamJYoM9kDh2Jvv4YZMawjbeeFtyOVLWZYYOqGJbmEYlRGRPhJSvRD16wBjSRMU9Ga1czPVV3g/640.png\n",
      "[OK] Uploading image url: http://api.pop.atomgit.com/admin/oss/getOssPathByUrl?url=https://mmbiz.qpic.cn/mmbiz_png/ibVdQeHFSRattLamJYoM9kDh2Jvv4YZMawjbeeFtyOVLWZYYOqGJbmEYlRGRPhJSvRD16wBjSRMU9Ga1czPVV3g/640.png\n",
      "[OK] Image uploaded successfully: https://file.pop.atomgit.com/imgs/openatom/2024/07/04/c1ffe9c14dd549d8aa4bf7161734c839.png\n",
      "[OK] Uploading image: https://mmbiz.qpic.cn/mmbiz_png/ibVdQeHFSRattLamJYoM9kDh2Jvv4YZManPWPb878Ls2VcS8958LwUqOMIfaTPLETEVicngGPzxdDMPewR88bajQ/640.png\n",
      "[OK] Uploading image url: http://api.pop.atomgit.com/admin/oss/getOssPathByUrl?url=https://mmbiz.qpic.cn/mmbiz_png/ibVdQeHFSRattLamJYoM9kDh2Jvv4YZManPWPb878Ls2VcS8958LwUqOMIfaTPLETEVicngGPzxdDMPewR88bajQ/640.png\n",
      "[OK] Image uploaded successfully: https://file.pop.atomgit.com/imgs/openatom/2024/07/04/f414217f1e7d403ab532b13eaf7784ed.png\n",
      "[OK] Uploading image: https://mmbiz.qpic.cn/mmbiz_png/ibVdQeHFSRattLamJYoM9kDh2Jvv4YZMa3JtndDITfvrBflIsnBnNe9rnGdyLgyObeur4wrENx5XluA73yOFxicg/640.png\n",
      "[OK] Uploading image url: http://api.pop.atomgit.com/admin/oss/getOssPathByUrl?url=https://mmbiz.qpic.cn/mmbiz_png/ibVdQeHFSRattLamJYoM9kDh2Jvv4YZMa3JtndDITfvrBflIsnBnNe9rnGdyLgyObeur4wrENx5XluA73yOFxicg/640.png\n",
      "[OK] Image uploaded successfully: https://file.pop.atomgit.com/imgs/openatom/2024/07/04/bcf807c0b6e14bf4b474574ce454ac87.png\n",
      "[OK] Uploading image: https://mmbiz.qpic.cn/mmbiz_png/ibVdQeHFSRattLamJYoM9kDh2Jvv4YZMaBq8icUplEdib020bbstumtlRlmQZj9YaMYsCPEmRmTnEpAtr36RZw2eA/640.png\n",
      "[OK] Uploading image url: http://api.pop.atomgit.com/admin/oss/getOssPathByUrl?url=https://mmbiz.qpic.cn/mmbiz_png/ibVdQeHFSRattLamJYoM9kDh2Jvv4YZMaBq8icUplEdib020bbstumtlRlmQZj9YaMYsCPEmRmTnEpAtr36RZw2eA/640.png\n",
      "[OK] Image uploaded successfully: https://file.pop.atomgit.com/imgs/openatom/2024/07/04/5bfb587d42a04c748d28182931b56a1c.png\n",
      "[OK] Uploading image: https://mmbiz.qpic.cn/mmbiz_png/ibVdQeHFSRattLamJYoM9kDh2Jvv4YZMa6BCt3HPNRSeNAgNcdgVkCTYt9Vhue6t6O7ictn7KH6eCsVwPlQ9qGibA/640.png\n",
      "[OK] Uploading image url: http://api.pop.atomgit.com/admin/oss/getOssPathByUrl?url=https://mmbiz.qpic.cn/mmbiz_png/ibVdQeHFSRattLamJYoM9kDh2Jvv4YZMa6BCt3HPNRSeNAgNcdgVkCTYt9Vhue6t6O7ictn7KH6eCsVwPlQ9qGibA/640.png\n",
      "[OK] Image uploaded successfully: https://file.pop.atomgit.com/imgs/openatom/2024/07/04/5c02d28cbe5d47a7925c78d6d07059c4.png\n",
      "[OK] Uploading image: https://mmbiz.qpic.cn/mmbiz_png/ibVdQeHFSRattLamJYoM9kDh2Jvv4YZManfvwl1eBAwGlkj65eFxQ9Qgqn7Pf6UxdR5ewKfMzib2HjibSWQ5LlyOA/640.png\n",
      "[OK] Uploading image url: http://api.pop.atomgit.com/admin/oss/getOssPathByUrl?url=https://mmbiz.qpic.cn/mmbiz_png/ibVdQeHFSRattLamJYoM9kDh2Jvv4YZManfvwl1eBAwGlkj65eFxQ9Qgqn7Pf6UxdR5ewKfMzib2HjibSWQ5LlyOA/640.png\n",
      "[OK] Image uploaded successfully: https://file.pop.atomgit.com/imgs/openatom/2024/07/04/e544b302a4544add9015caca57bb7233.png\n",
      "[OK] Uploading image: https://mmbiz.qpic.cn/mmbiz_png/ibVdQeHFSRattLamJYoM9kDh2Jvv4YZMa6CaXhJnOA3QCEOLiajTQy4D2icVhWk6JMa4Doy8UEArDn3pRk53U3zicQ/640.png\n",
      "[OK] Uploading image url: http://api.pop.atomgit.com/admin/oss/getOssPathByUrl?url=https://mmbiz.qpic.cn/mmbiz_png/ibVdQeHFSRattLamJYoM9kDh2Jvv4YZMa6CaXhJnOA3QCEOLiajTQy4D2icVhWk6JMa4Doy8UEArDn3pRk53U3zicQ/640.png\n",
      "[OK] Image uploaded successfully: https://file.pop.atomgit.com/imgs/openatom/2024/07/04/c000f400f6d04575a62033e8a1363939.png\n",
      "[OK] Uploading image: https://mmbiz.qpic.cn/mmbiz_png/ibVdQeHFSRattLamJYoM9kDh2Jvv4YZMa2lnJI8lrK7s7XpfibKUXYwSGpyJqOib3bKXkjDPzVJVfVONensj9dNMw/640.png\n",
      "[OK] Uploading image url: http://api.pop.atomgit.com/admin/oss/getOssPathByUrl?url=https://mmbiz.qpic.cn/mmbiz_png/ibVdQeHFSRattLamJYoM9kDh2Jvv4YZMa2lnJI8lrK7s7XpfibKUXYwSGpyJqOib3bKXkjDPzVJVfVONensj9dNMw/640.png\n",
      "[OK] Image uploaded successfully: https://file.pop.atomgit.com/imgs/openatom/2024/07/04/dde756732d63486abde622e4d02511a0.png\n",
      "[OK] Uploading image: https://mmbiz.qpic.cn/mmbiz_png/ibVdQeHFSRattLamJYoM9kDh2Jvv4YZMaURiaCCO0vVRImmnDcJwpvtcz8ajHTZGMoSNySYsLyfkIBoiaYXDYsNPQ/640.png\n",
      "[OK] Uploading image url: http://api.pop.atomgit.com/admin/oss/getOssPathByUrl?url=https://mmbiz.qpic.cn/mmbiz_png/ibVdQeHFSRattLamJYoM9kDh2Jvv4YZMaURiaCCO0vVRImmnDcJwpvtcz8ajHTZGMoSNySYsLyfkIBoiaYXDYsNPQ/640.png\n",
      "[OK] Image uploaded successfully: https://file.pop.atomgit.com/imgs/openatom/2024/07/04/7ef9cb6a056a4d149b2cc70f671d4ea8.png\n",
      "[OK] Uploading image: https://mmbiz.qpic.cn/mmbiz_png/ibVdQeHFSRattLamJYoM9kDh2Jvv4YZMa6GpYNYecY6M92SXmVMnibH57CDNhyJ0dDrgk0ibXN6thGNFAFsAN2thw/640.png\n",
      "[OK] Uploading image url: http://api.pop.atomgit.com/admin/oss/getOssPathByUrl?url=https://mmbiz.qpic.cn/mmbiz_png/ibVdQeHFSRattLamJYoM9kDh2Jvv4YZMa6GpYNYecY6M92SXmVMnibH57CDNhyJ0dDrgk0ibXN6thGNFAFsAN2thw/640.png\n",
      "[OK] Image uploaded successfully: https://file.pop.atomgit.com/imgs/openatom/2024/07/04/4bfdcbb7921444e1b06b300eac46e718.png\n",
      "[OK] Uploading image: https://mmbiz.qpic.cn/mmbiz_png/ibVdQeHFSRattLamJYoM9kDh2Jvv4YZMaaWuenia2bXRSoezQgITWMgbTECicIRqlatBlzsxksHP1lS3RhWRYxXSg/640.png\n",
      "[OK] Uploading image url: http://api.pop.atomgit.com/admin/oss/getOssPathByUrl?url=https://mmbiz.qpic.cn/mmbiz_png/ibVdQeHFSRattLamJYoM9kDh2Jvv4YZMaaWuenia2bXRSoezQgITWMgbTECicIRqlatBlzsxksHP1lS3RhWRYxXSg/640.png\n",
      "[OK] Image uploaded successfully: https://file.pop.atomgit.com/imgs/openatom/2024/07/04/9a3fb0e248bf48a09764de0d69a3d1af.png\n",
      "[OK] Uploading image: https://mmbiz.qpic.cn/mmbiz_png/9joZrzibB9jSRPMmMGibV0uWticqDtNSYafhZHWJp6xo4IiaXZ5c4hhW1T3cfrI05heicg6o21ZTeTAge6TnC86yY4A/640.png\n",
      "[OK] Uploading image url: http://api.pop.atomgit.com/admin/oss/getOssPathByUrl?url=https://mmbiz.qpic.cn/mmbiz_png/9joZrzibB9jSRPMmMGibV0uWticqDtNSYafhZHWJp6xo4IiaXZ5c4hhW1T3cfrI05heicg6o21ZTeTAge6TnC86yY4A/640.png\n",
      "[OK] Image uploaded successfully: https://file.pop.atomgit.com/imgs/openatom/2024/07/04/b0195dbb51294892a0337f5a772dfc7a.png\n",
      "[OK] Image URLs replaced successfully.\n",
      "[OK] Publishing content...\n",
      "[OK] Content published successfully.\n",
      "[OK] All Done.\n"
     ]
    }
   ],
   "source": [
    "import requests\n",
    "import re\n",
    "import json\n",
    "import os\n",
    "from datetime import datetime\n",
    "\n",
    "def load_headers():\n",
    "    try:\n",
    "        with open('header.json', 'r', encoding='utf-8') as f:\n",
    "            headers = json.load(f)\n",
    "        print(\"[OK] Headers loaded successfully from header.json.\")\n",
    "        return headers\n",
    "    except Exception as e:\n",
    "        print(f\"[Failed] Failed to load headers from header.json. Error: {e}\")\n",
    "        return {}\n",
    "\n",
    "def upload_image(image_url, headers):\n",
    "    try:\n",
    "        print(f\"[OK] Uploading image: {image_url}\")\n",
    "        url = f\"http://api.pop.atomgit.com/admin/oss/getOssPathByUrl?url={image_url}\"\n",
    "        print(f\"[OK] Uploading image url: {url}\")\n",
    "        \n",
    "        response = requests.get(url, headers=headers)\n",
    "        \n",
    "        if response.status_code == 200:\n",
    "            new_url = response.json().get('data')\n",
    "            if new_url:\n",
    "                print(f\"[OK] Image uploaded successfully: {new_url}\")\n",
    "                return new_url\n",
    "            else:\n",
    "                raise Exception(\"No URL returned from server\")\n",
    "        else:\n",
    "            raise Exception(f\"Failed to upload image. Status code: {response.status_code}, response: {response.text}\")\n",
    "    except Exception as e:\n",
    "        print(f\"[Failed] An error occurred while uploading image: {e}\")\n",
    "        return image_url  # Return original URL if upload fails\n",
    "\n",
    "def replace_image_urls(content, headers):\n",
    "    # Find all image URLs in the content\n",
    "    image_urls = re.findall(r'<img[^>]+src=\"([^\">]+)\"', content)\n",
    "    for image_url in image_urls:\n",
    "        # Split the URL to separate the base URL and the query string\n",
    "        parts = re.split(r'\\?(.*)', image_url)\n",
    "        base_image_url = parts[0]\n",
    "        query_string = '?' + parts[1] if len(parts) > 1 else ''\n",
    "        \n",
    "        # Replace '?' and everything after it with '.png' for the base URL\n",
    "        clean_image_url = base_image_url + '.png'\n",
    "        \n",
    "        # Upload the image and get the new URL\n",
    "        new_url = upload_image(clean_image_url, headers)\n",
    "        \n",
    "        # Append the query string back to the new URL\n",
    "        if query_string:\n",
    "            new_url += query_string\n",
    "        \n",
    "        # Replace the old image URL with the new URL in the content\n",
    "        content = content.replace(image_url, new_url)\n",
    "    \n",
    "    return content\n",
    "\n",
    "def publish_content(title, message, content, headers):\n",
    "    try:\n",
    "        print(\"[OK] Publishing content...\")\n",
    "        url = 'https://openatom.atomgit.com/api/admin/information/create'\n",
    "        data = {\n",
    "            \"informationTitle\": title,\n",
    "            \"informationMsg\": message,\n",
    "            \"labelIdList\": [\"5\"],\n",
    "            \"coverUrl\": \"https://file.pop.atomgit.com/imgs/openatom/2024/06/19/676d249f47658b8aabc36611b608c385.png\",\n",
    "            \"informationContent\": content,\n",
    "            \"informationType\": 2,\n",
    "            \"informationState\": 2\n",
    "        }\n",
    "        response = requests.post(url, headers=headers, data=json.dumps(data))\n",
    "\n",
    "        if response.status_code == 200:\n",
    "            try:\n",
    "                if response.json()[\"code\"] != 400101:\n",
    "                    print(\"[OK] Content published successfully.\")\n",
    "                else:\n",
    "                    print(f\"[Failed] Failed to publish content. Status code: {response.status_code}, response: {response.text}\")\n",
    "            except json.JSONDecodeError:\n",
    "                print(f\"[Failed] Invalid JSON response: {response.text}\")\n",
    "        else:\n",
    "            print(f\"[Failed] Failed to publish content. Status code: {response.status_code}, response: {response.text}\")\n",
    "    except Exception as e:\n",
    "        print(f\"[Failed] An error occurred while publishing content: {e}\")\n",
    "\n",
    "def main():\n",
    "    try:\n",
    "        # 获取当前日期\n",
    "        current_date = datetime.now().strftime('%Y%m%d')\n",
    "\n",
    "        # 加载 headers\n",
    "        headers = load_headers()\n",
    "\n",
    "        # 读取路径下的所有 txt 文件\n",
    "        output_dir = os.path.join('output', current_date)\n",
    "        if not os.path.exists(output_dir):\n",
    "            raise Exception(f\"Directory {output_dir} does not exist.\")\n",
    "        \n",
    "        txt_files = [f for f in os.listdir(output_dir) if f.endswith('.txt')]\n",
    "        txt_files.sort(key=lambda f: int(re.match(r'(\\d+)', f).group()))  # 按数字顺序排序\n",
    "\n",
    "        for txt_file in txt_files:\n",
    "            file_path = os.path.join(output_dir, txt_file)\n",
    "            print(f\"[OK] Reading content from {file_path}...\")\n",
    "            with open(file_path, 'r', encoding='utf-8') as f:\n",
    "                lines = f.readlines()\n",
    "            \n",
    "            print(f\"[OK] Content read successfully from {file_path}.\")\n",
    "\n",
    "            # 提取标题和消息\n",
    "            title_line = lines[0].strip()\n",
    "            url_line = lines[1].strip()\n",
    "            title = re.sub(r'<[^>]+>', '', title_line.replace('Title: ', ''))\n",
    "            url_source = re.sub(r'<[^>]+>', '', url_line.replace('URL Source: ', ''))\n",
    "\n",
    "            # 提取正文内容\n",
    "            content_lines = lines[3:]  # 跳过前面的三行\n",
    "            content = ''.join(content_lines)\n",
    "            \n",
    "            # 在正文末尾添加文章来源\n",
    "            content += f'---\\n\\n文章来源：{url_source}'\n",
    "\n",
    "            # 替换图片 URL\n",
    "            print(\"[OK] Replacing image URLs...\")\n",
    "            updated_content = replace_image_urls(content, headers)\n",
    "            print(\"[OK] Image URLs replaced successfully.\")\n",
    "\n",
    "            # 发布内容\n",
    "            publish_content(title, title, updated_content, headers)\n",
    "\n",
    "    except Exception as e:\n",
    "        print(f\"[Failed] An error occurred in main: {e}\")\n",
    "\n",
    "    print(\"[OK] All Done.\")\n",
    "\n",
    "if __name__ == \"__main__\":\n",
    "    main()\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1e63406e-b367-477a-a338-e85fafc64635",
   "metadata": {},
   "source": [
    "# 3、发文章到 AtomGit 探索：开源资讯"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "1817570c-f574-4bb5-8b03-c296301876d0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[OK] Headers loaded successfully from header.json.\n",
      "[OK] Reading content from output/20240704/1.txt...\n",
      "[OK] Content read successfully from output/20240704/1.txt.\n",
      "[OK] Replacing image URLs...\n",
      "[OK] Uploading image: https://mmbiz.qpic.cn/mmbiz_png/ibVdQeHFSRattLamJYoM9kDh2Jvv4YZMajR4WvtnlGeG3FnwGvtSOPza9rsppmdzNoGpSeJHOWISqxGicyjq4lQQ/640.png\n",
      "[OK] Uploading image URL: http://api.pop.atomgit.com/admin/oss/getOssPathByUrl?url=https://mmbiz.qpic.cn/mmbiz_png/ibVdQeHFSRattLamJYoM9kDh2Jvv4YZMajR4WvtnlGeG3FnwGvtSOPza9rsppmdzNoGpSeJHOWISqxGicyjq4lQQ/640.png\n",
      "[OK] Image uploaded successfully: https://file.pop.atomgit.com/imgs/openatom/2024/07/04/1467976fd8e3442db87fc60e5c00dcf6.png\n",
      "[OK] Uploading image: https://mmbiz.qpic.cn/mmbiz_png/ibVdQeHFSRattLamJYoM9kDh2Jvv4YZMaia3XpmkXGWupfyqjFNw5ibJiaibibbhZIoQiacBuuWUdFFGEUMwRt9Bxf1JQ/640.png\n",
      "[OK] Uploading image URL: http://api.pop.atomgit.com/admin/oss/getOssPathByUrl?url=https://mmbiz.qpic.cn/mmbiz_png/ibVdQeHFSRattLamJYoM9kDh2Jvv4YZMaia3XpmkXGWupfyqjFNw5ibJiaibibbhZIoQiacBuuWUdFFGEUMwRt9Bxf1JQ/640.png\n",
      "[OK] Image uploaded successfully: https://file.pop.atomgit.com/imgs/openatom/2024/07/04/f41c007a17f74ca69695ba24e6527819.png\n",
      "[OK] Uploading image: https://mmbiz.qpic.cn/mmbiz_png/ibVdQeHFSRattLamJYoM9kDh2Jvv4YZMatRFfH3C2fVOoNUneBbjDOib6c2pZ654IRFvGen8of1SlJh3DB3td26w/640.png\n",
      "[OK] Uploading image URL: http://api.pop.atomgit.com/admin/oss/getOssPathByUrl?url=https://mmbiz.qpic.cn/mmbiz_png/ibVdQeHFSRattLamJYoM9kDh2Jvv4YZMatRFfH3C2fVOoNUneBbjDOib6c2pZ654IRFvGen8of1SlJh3DB3td26w/640.png\n",
      "[OK] Image uploaded successfully: https://file.pop.atomgit.com/imgs/openatom/2024/07/04/3c95bc2106314d6086ef1363ee62dd09.png\n",
      "[OK] Uploading image: https://mmbiz.qpic.cn/mmbiz_png/ibVdQeHFSRattLamJYoM9kDh2Jvv4YZMawjbeeFtyOVLWZYYOqGJbmEYlRGRPhJSvRD16wBjSRMU9Ga1czPVV3g/640.png\n",
      "[OK] Uploading image URL: http://api.pop.atomgit.com/admin/oss/getOssPathByUrl?url=https://mmbiz.qpic.cn/mmbiz_png/ibVdQeHFSRattLamJYoM9kDh2Jvv4YZMawjbeeFtyOVLWZYYOqGJbmEYlRGRPhJSvRD16wBjSRMU9Ga1czPVV3g/640.png\n",
      "[OK] Image uploaded successfully: https://file.pop.atomgit.com/imgs/openatom/2024/07/04/ac2cd5c32b54437ca0576ee7af4b73b3.png\n",
      "[OK] Uploading image: https://mmbiz.qpic.cn/mmbiz_png/ibVdQeHFSRattLamJYoM9kDh2Jvv4YZManPWPb878Ls2VcS8958LwUqOMIfaTPLETEVicngGPzxdDMPewR88bajQ/640.png\n",
      "[OK] Uploading image URL: http://api.pop.atomgit.com/admin/oss/getOssPathByUrl?url=https://mmbiz.qpic.cn/mmbiz_png/ibVdQeHFSRattLamJYoM9kDh2Jvv4YZManPWPb878Ls2VcS8958LwUqOMIfaTPLETEVicngGPzxdDMPewR88bajQ/640.png\n",
      "[OK] Image uploaded successfully: https://file.pop.atomgit.com/imgs/openatom/2024/07/04/0103f3fe802f40a78a9bbd641c85e975.png\n",
      "[OK] Uploading image: https://mmbiz.qpic.cn/mmbiz_png/ibVdQeHFSRattLamJYoM9kDh2Jvv4YZMa3JtndDITfvrBflIsnBnNe9rnGdyLgyObeur4wrENx5XluA73yOFxicg/640.png\n",
      "[OK] Uploading image URL: http://api.pop.atomgit.com/admin/oss/getOssPathByUrl?url=https://mmbiz.qpic.cn/mmbiz_png/ibVdQeHFSRattLamJYoM9kDh2Jvv4YZMa3JtndDITfvrBflIsnBnNe9rnGdyLgyObeur4wrENx5XluA73yOFxicg/640.png\n",
      "[OK] Image uploaded successfully: https://file.pop.atomgit.com/imgs/openatom/2024/07/04/f5db484f9110486fa96ce6cb186eb10b.png\n",
      "[OK] Uploading image: https://mmbiz.qpic.cn/mmbiz_png/ibVdQeHFSRattLamJYoM9kDh2Jvv4YZMaBq8icUplEdib020bbstumtlRlmQZj9YaMYsCPEmRmTnEpAtr36RZw2eA/640.png\n",
      "[OK] Uploading image URL: http://api.pop.atomgit.com/admin/oss/getOssPathByUrl?url=https://mmbiz.qpic.cn/mmbiz_png/ibVdQeHFSRattLamJYoM9kDh2Jvv4YZMaBq8icUplEdib020bbstumtlRlmQZj9YaMYsCPEmRmTnEpAtr36RZw2eA/640.png\n",
      "[OK] Image uploaded successfully: https://file.pop.atomgit.com/imgs/openatom/2024/07/04/d7b1eed6abd2490ebb6bcb19d07396e0.png\n",
      "[OK] Uploading image: https://mmbiz.qpic.cn/mmbiz_png/ibVdQeHFSRattLamJYoM9kDh2Jvv4YZMa6BCt3HPNRSeNAgNcdgVkCTYt9Vhue6t6O7ictn7KH6eCsVwPlQ9qGibA/640.png\n",
      "[OK] Uploading image URL: http://api.pop.atomgit.com/admin/oss/getOssPathByUrl?url=https://mmbiz.qpic.cn/mmbiz_png/ibVdQeHFSRattLamJYoM9kDh2Jvv4YZMa6BCt3HPNRSeNAgNcdgVkCTYt9Vhue6t6O7ictn7KH6eCsVwPlQ9qGibA/640.png\n",
      "[OK] Image uploaded successfully: https://file.pop.atomgit.com/imgs/openatom/2024/07/04/9abb97f684634c80bfc6dff2ed6a88d6.png\n",
      "[OK] Uploading image: https://mmbiz.qpic.cn/mmbiz_png/ibVdQeHFSRattLamJYoM9kDh2Jvv4YZManfvwl1eBAwGlkj65eFxQ9Qgqn7Pf6UxdR5ewKfMzib2HjibSWQ5LlyOA/640.png\n",
      "[OK] Uploading image URL: http://api.pop.atomgit.com/admin/oss/getOssPathByUrl?url=https://mmbiz.qpic.cn/mmbiz_png/ibVdQeHFSRattLamJYoM9kDh2Jvv4YZManfvwl1eBAwGlkj65eFxQ9Qgqn7Pf6UxdR5ewKfMzib2HjibSWQ5LlyOA/640.png\n",
      "[OK] Image uploaded successfully: https://file.pop.atomgit.com/imgs/openatom/2024/07/04/9ba931d159b541f49c732d2131a4919b.png\n",
      "[OK] Uploading image: https://mmbiz.qpic.cn/mmbiz_png/ibVdQeHFSRattLamJYoM9kDh2Jvv4YZMa6CaXhJnOA3QCEOLiajTQy4D2icVhWk6JMa4Doy8UEArDn3pRk53U3zicQ/640.png\n",
      "[OK] Uploading image URL: http://api.pop.atomgit.com/admin/oss/getOssPathByUrl?url=https://mmbiz.qpic.cn/mmbiz_png/ibVdQeHFSRattLamJYoM9kDh2Jvv4YZMa6CaXhJnOA3QCEOLiajTQy4D2icVhWk6JMa4Doy8UEArDn3pRk53U3zicQ/640.png\n",
      "[OK] Image uploaded successfully: https://file.pop.atomgit.com/imgs/openatom/2024/07/04/710a95157ab64bdbbbc09cbd9c779b41.png\n",
      "[OK] Uploading image: https://mmbiz.qpic.cn/mmbiz_png/ibVdQeHFSRattLamJYoM9kDh2Jvv4YZMa2lnJI8lrK7s7XpfibKUXYwSGpyJqOib3bKXkjDPzVJVfVONensj9dNMw/640.png\n",
      "[OK] Uploading image URL: http://api.pop.atomgit.com/admin/oss/getOssPathByUrl?url=https://mmbiz.qpic.cn/mmbiz_png/ibVdQeHFSRattLamJYoM9kDh2Jvv4YZMa2lnJI8lrK7s7XpfibKUXYwSGpyJqOib3bKXkjDPzVJVfVONensj9dNMw/640.png\n",
      "[OK] Image uploaded successfully: https://file.pop.atomgit.com/imgs/openatom/2024/07/04/88bd311638fb478db469eca9b209ddf4.png\n",
      "[OK] Uploading image: https://mmbiz.qpic.cn/mmbiz_png/ibVdQeHFSRattLamJYoM9kDh2Jvv4YZMaURiaCCO0vVRImmnDcJwpvtcz8ajHTZGMoSNySYsLyfkIBoiaYXDYsNPQ/640.png\n",
      "[OK] Uploading image URL: http://api.pop.atomgit.com/admin/oss/getOssPathByUrl?url=https://mmbiz.qpic.cn/mmbiz_png/ibVdQeHFSRattLamJYoM9kDh2Jvv4YZMaURiaCCO0vVRImmnDcJwpvtcz8ajHTZGMoSNySYsLyfkIBoiaYXDYsNPQ/640.png\n",
      "[OK] Image uploaded successfully: https://file.pop.atomgit.com/imgs/openatom/2024/07/04/6e6ce54b06bf4ad69311905569361844.png\n",
      "[OK] Uploading image: https://mmbiz.qpic.cn/mmbiz_png/ibVdQeHFSRattLamJYoM9kDh2Jvv4YZMa6GpYNYecY6M92SXmVMnibH57CDNhyJ0dDrgk0ibXN6thGNFAFsAN2thw/640.png\n",
      "[OK] Uploading image URL: http://api.pop.atomgit.com/admin/oss/getOssPathByUrl?url=https://mmbiz.qpic.cn/mmbiz_png/ibVdQeHFSRattLamJYoM9kDh2Jvv4YZMa6GpYNYecY6M92SXmVMnibH57CDNhyJ0dDrgk0ibXN6thGNFAFsAN2thw/640.png\n",
      "[OK] Image uploaded successfully: https://file.pop.atomgit.com/imgs/openatom/2024/07/04/08e33d93ce1647cbbc3daffa7b987cac.png\n",
      "[OK] Uploading image: https://mmbiz.qpic.cn/mmbiz_png/ibVdQeHFSRattLamJYoM9kDh2Jvv4YZMaaWuenia2bXRSoezQgITWMgbTECicIRqlatBlzsxksHP1lS3RhWRYxXSg/640.png\n",
      "[OK] Uploading image URL: http://api.pop.atomgit.com/admin/oss/getOssPathByUrl?url=https://mmbiz.qpic.cn/mmbiz_png/ibVdQeHFSRattLamJYoM9kDh2Jvv4YZMaaWuenia2bXRSoezQgITWMgbTECicIRqlatBlzsxksHP1lS3RhWRYxXSg/640.png\n",
      "[OK] Image uploaded successfully: https://file.pop.atomgit.com/imgs/openatom/2024/07/04/47d310f5d2b348b1a6af14a23d36f3ab.png\n",
      "[OK] Uploading image: https://mmbiz.qpic.cn/mmbiz_png/9joZrzibB9jSRPMmMGibV0uWticqDtNSYafhZHWJp6xo4IiaXZ5c4hhW1T3cfrI05heicg6o21ZTeTAge6TnC86yY4A/640.png\n",
      "[OK] Uploading image URL: http://api.pop.atomgit.com/admin/oss/getOssPathByUrl?url=https://mmbiz.qpic.cn/mmbiz_png/9joZrzibB9jSRPMmMGibV0uWticqDtNSYafhZHWJp6xo4IiaXZ5c4hhW1T3cfrI05heicg6o21ZTeTAge6TnC86yY4A/640.png\n",
      "[OK] Image uploaded successfully: https://file.pop.atomgit.com/imgs/openatom/2024/07/04/8d9b1d20ae3b4db5bede31f18a5cab4e.png\n",
      "[OK] Image URLs replaced successfully.\n",
      "[OK] Publishing content...\n",
      "[OK] Content published successfully.\n",
      "[OK] Finished processing output/20240704/1.txt.\n",
      "[OK] All Done.\n"
     ]
    }
   ],
   "source": [
    "import requests\n",
    "import re\n",
    "import json\n",
    "import os\n",
    "from datetime import datetime\n",
    "\n",
    "def load_headers():\n",
    "    try:\n",
    "        with open('header.json', 'r', encoding='utf-8') as f:\n",
    "            headers = json.load(f)\n",
    "        print(\"[OK] Headers loaded successfully from header.json.\")\n",
    "        return headers\n",
    "    except Exception as e:\n",
    "        print(f\"[Failed] Failed to load headers from header.json. Error: {e}\")\n",
    "        return None\n",
    "\n",
    "def upload_image(image_url, headers):\n",
    "    try:\n",
    "        print(f\"[OK] Uploading image: {image_url}\")\n",
    "        url = f\"http://api.pop.atomgit.com/admin/oss/getOssPathByUrl?url={image_url}\"\n",
    "        print(f\"[OK] Uploading image URL: {url}\")\n",
    "        \n",
    "        response = requests.get(url, headers=headers)\n",
    "        \n",
    "        if response.status_code == 200:\n",
    "            new_url = response.json().get('data')\n",
    "            if new_url:\n",
    "                print(f\"[OK] Image uploaded successfully: {new_url}\")\n",
    "                return new_url\n",
    "            else:\n",
    "                raise Exception(\"No URL returned from server\")\n",
    "        else:\n",
    "            raise Exception(f\"Failed to upload image. Status code: {response.status_code}, response: {response.text}\")\n",
    "    except Exception as e:\n",
    "        print(f\"[Failed] An error occurred while uploading image: {e}\")\n",
    "        return image_url  # Return original URL if upload fails\n",
    "\n",
    "def replace_image_urls(content, headers):\n",
    "    # Find all image URLs in the content\n",
    "    image_urls = re.findall(r'<img[^>]+src=\"([^\">]+)\"', content)\n",
    "    for image_url in image_urls:\n",
    "        # Split the URL to separate the base URL and the query string\n",
    "        parts = re.split(r'\\?(.*)', image_url)\n",
    "        base_image_url = parts[0]\n",
    "        query_string = '?' + parts[1] if len(parts) > 1 else ''\n",
    "        \n",
    "        # Replace '?' and everything after it with '.png' for the base URL\n",
    "        clean_image_url = base_image_url + '.png'\n",
    "        \n",
    "        # Upload the image and get the new URL\n",
    "        new_url = upload_image(clean_image_url, headers)\n",
    "        \n",
    "        # Append the query string back to the new URL\n",
    "        if query_string:\n",
    "            new_url += query_string\n",
    "        \n",
    "        # Replace the old image URL with the new URL in the content\n",
    "        content = content.replace(image_url, new_url)\n",
    "    \n",
    "    return content\n",
    "\n",
    "def publish_content(headers, title, content, summary, cover):\n",
    "    try:\n",
    "        print(\"[OK] Publishing content...\")\n",
    "        url = 'https://atomgit.com/api/admin/articalInfo/v1/save'\n",
    "        data = {\n",
    "            \"title\": title,\n",
    "            \"content\": content,\n",
    "            \"summary\": summary,\n",
    "            \"type\": 2,\n",
    "            \"city\": \"\",\n",
    "            \"place\": \"\",\n",
    "            \"cover\": cover,\n",
    "            \"jumpUrl\": \"\",\n",
    "            \"activityType\": \"\",\n",
    "            \"beginTime\": \"\",\n",
    "            \"endTime\": \"\",\n",
    "            \"activityDate\": \"\",\n",
    "            \"province\": \"\",\n",
    "            \"country\": \"\"\n",
    "        }\n",
    "        response = requests.post(url, headers=headers, data=json.dumps(data))\n",
    "        if response.status_code == 200:\n",
    "            print(\"[OK] Content published successfully.\")\n",
    "        else:\n",
    "            print(f\"[Failed] Failed to publish content. Status code: {response.status_code}, response: {response.text}\")\n",
    "    except Exception as e:\n",
    "        print(f\"[Failed] An error occurred while publishing content: {e}\")\n",
    "\n",
    "def main():\n",
    "    try:\n",
    "        # Load headers\n",
    "        headers = load_headers()\n",
    "        if not headers:\n",
    "            print(\"[Failed] Exiting due to failed headers loading.\")\n",
    "            return\n",
    "\n",
    "        # Get current date\n",
    "        current_date = datetime.now().strftime('%Y%m%d')\n",
    "        output_dir = os.path.join('output', current_date)\n",
    "        \n",
    "        # Ensure the output directory exists\n",
    "        if not os.path.exists(output_dir):\n",
    "            print(f\"[Failed] Output directory does not exist: {output_dir}\")\n",
    "            return\n",
    "\n",
    "        # List all text files in the output directory\n",
    "        txt_files = sorted([f for f in os.listdir(output_dir) if f.endswith('.txt')], key=lambda x: int(x.split('.')[0]))\n",
    "\n",
    "        for idx, txt_file in enumerate(txt_files, start=1):\n",
    "            file_path = os.path.join(output_dir, txt_file)\n",
    "\n",
    "            print(f\"[OK] Reading content from {file_path}...\")\n",
    "            with open(file_path, 'r', encoding='utf-8') as f:\n",
    "                lines = f.readlines()\n",
    "            \n",
    "            print(f\"[OK] Content read successfully from {file_path}.\")\n",
    "\n",
    "            # Extract title and URL source\n",
    "            title_line = lines[0].strip()\n",
    "            url_line = lines[1].strip()\n",
    "            title = re.sub(r'<[^>]+>', '', title_line.replace('Title: ', ''))\n",
    "            url_source = re.sub(r'<[^>]+>', '', url_line.replace('URL Source: ', ''))\n",
    "\n",
    "            # Extract body content\n",
    "            content_lines = lines[3:]  # Skip the first three lines\n",
    "            content = ''.join(content_lines)\n",
    "            \n",
    "            # Append source URL to content\n",
    "            content += f'---\\n\\n文章来源：{url_source}'\n",
    "\n",
    "            # Replace image URLs\n",
    "            print(\"[OK] Replacing image URLs...\")\n",
    "            updated_content = replace_image_urls(content, headers)\n",
    "            print(\"[OK] Image URLs replaced successfully.\")\n",
    "\n",
    "            # Set cover image URL if available\n",
    "            cover_url = \"https://file.pop.atomgit.com/imgs/openatom/2024/06/19/676d249f47658b8aabc36611b608c385.png\"\n",
    "\n",
    "            # Publish content\n",
    "            publish_content(headers, title, updated_content, title, cover_url)\n",
    "\n",
    "            print(f\"[OK] Finished processing {file_path}.\")\n",
    "\n",
    "    except Exception as e:\n",
    "        print(f\"[Failed] An error occurred in main: {e}\")\n",
    "\n",
    "    print(\"[OK] All Done.\")\n",
    "\n",
    "if __name__ == \"__main__\":\n",
    "    main()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3de45e77-e1a6-4240-ab77-4a9466d01968",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.7"
  },
  "widgets": {
   "application/vnd.jupyter.widget-state+json": {
    "state": {},
    "version_major": 2,
    "version_minor": 0
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
